#include "toolselect.h"
#include <QMouseEvent>
#include <Document>
#include "application.h"
#include "src/selection/elementselector.h"
#include "src/selection/selectionmanager.h"
#include "src/drawingview.h"

#include <QPushButton>
#include <QVBoxLayout>
#include <QGroupBox>
#include <QButtonGroup>

ToolSelect::ToolSelect(QObject *parent) :
    Tool(parent)
{
    m_selector = new ElementSelector(ElementSelector::Rectangle, this);
}

QString ToolSelect::name() const
{
    return "Select";
}

QString ToolSelect::categoryName() const
{
    return "Transform";
}

QString ToolSelect::iconPath() const
{
    return "resources\\icons\\select.png";
}

void ToolSelect::enter()
{

}

void ToolSelect::exit()
{

}

void ToolSelect::onMousePress(QMouseEvent *pEvent)
{
    QPointer<DrawingView> activeView = qobject_cast<DrawingView*>(Application::app().activeDocument()->ownerView());
    Q_ASSERT(activeView);

    QPointer<SelectionManager> selManager = activeView->selectionManager();
    Q_ASSERT(selManager);

    selManager->setSelector(m_selector);
    m_selector->mousePress(pEvent);
    selManager->updateSelection();
    activeView->update();
}

void ToolSelect::onMouseMove(QMouseEvent *pEvent)
{
    QPointer<DrawingView> activeView = qobject_cast<DrawingView*>(Application::app().activeDocument()->ownerView());
    Q_ASSERT(activeView);

    QPointer<SelectionManager> selManager = activeView->selectionManager();
    Q_ASSERT(selManager);

    m_selector->mouseMove(pEvent);
    selManager->updateSelection();
    activeView->update();
}

void ToolSelect::onMouseRelease(QMouseEvent *pEvent)
{
    QPointer<DrawingView> activeView = qobject_cast<DrawingView*>(Application::app().activeDocument()->ownerView());
    Q_ASSERT(activeView);

    QPointer<SelectionManager> selManager = activeView->selectionManager();
    Q_ASSERT(selManager);

    m_selector->mouseRelease(pEvent);

    selManager->setSelector(0);
    activeView->update();
}

void ToolSelect::initPropertiesWidget()
{
    m_propertiesWidget = new QGroupBox("Selection Properties");
    QVBoxLayout* pLayout = new QVBoxLayout();
    m_propertiesWidget->setLayout(pLayout);

    m_buttonRectangle = new QPushButton("Rectangle");
    m_buttonRectangle->setCheckable(true);
    m_buttonCircle = new QPushButton("Circle");
    m_buttonCircle->setCheckable(true);
    m_buttonPolygon = new QPushButton("Polygon");
    m_buttonPolygon->setCheckable(true);

    pLayout->addWidget(m_buttonRectangle);
    pLayout->addWidget(m_buttonCircle);
    pLayout->addWidget(m_buttonPolygon);

    QButtonGroup* pBGroup = new QButtonGroup(this);

    pBGroup->addButton(m_buttonRectangle);
    pBGroup->addButton(m_buttonCircle);
    pBGroup->addButton(m_buttonPolygon);

    connect(m_buttonRectangle, SIGNAL(clicked()), this, SLOT(selectionModeChanged()));
    connect(m_buttonCircle, SIGNAL(clicked()), this, SLOT(selectionModeChanged()));
    connect(m_buttonPolygon, SIGNAL(clicked()), this, SLOT(selectionModeChanged()));

    m_buttonRectangle->click();
}

void ToolSelect::selectionModeChanged()
{
    int type = 0;

    QPushButton* pSender = qobject_cast<QPushButton*>(sender());

    if(pSender == m_buttonRectangle)
    {
        type = 0;
    }
    else if(pSender == m_buttonCircle)
    {
        type = 1;
    }
    else if(pSender = m_buttonPolygon)
    {
        type = 2;
    }

    m_selector->setSelectionType(ElementSelector::SelectionType(type));
}

